#import "ScCardScanner.h"
#import "IOSOCRAPI.h"

#import <Cordova/CDVPluginResult.h>

@implementation ScCardScanner

- (void)init:(CDVInvokedUrlCommand*)command
{
    int ret = TREC_StartUP();
    CDVPluginResult* result;
    if (ret == 100) {
        NSLog(@"引擎过期\n");
        result = [CDVPluginResult
                  resultWithStatus: CDVCommandStatus_ERROR
                  messageAsString: @"引擎过期"
                  ];
    } else {
        result = [CDVPluginResult
                  resultWithStatus: CDVCommandStatus_OK
                  ];
    }
    [[self commandDelegate] sendPluginResult:result callbackId: self.callback];
}

- (void)scan:(CDVInvokedUrlCommand*)command
{
    NSArray  *arguments = command.arguments;
    self.callback = command.callbackId; //初始化返回信息
    self.resultDict = [[NSMutableDictionary alloc] init];
    NSDictionary* params = arguments[0];
    int *type = [[params valueForKey:@"type"] intValue];
    if (type == 1) { //身份证
        SCCaptureCameraController *con = [[SCCaptureCameraController alloc] init];
        con.scNaigationDelegate = self;
        con.iCardType = TIDCARD2;
        con.isDisPlayTxt = YES;
        con.ScanMode = TIDC_SCAN_MODE;
        [self.viewController presentViewController:con animated:YES completion:NULL];
    } else if (type == 4) { //银行卡
        SCCaptureCameraController *con = [[SCCaptureCameraController alloc] init];
        con.scNaigationDelegate = self;
        con.iCardType = TIDBANK;
        con.isDisPlayTxt = YES;
        [self.viewController presentViewController:con animated:YES completion:NULL];
    } else if (type == 5) {//车牌
        SCCaptureCameraController *con = [[SCCaptureCameraController alloc] init];
        con.scNaigationDelegate = self;
        con.iCardType = TIDLPR;
        con.isDisPlayTxt = YES;
        [self.viewController presentViewController:con animated:YES completion:NULL];
    } else if (type == 3) { //行驶证
        SCCaptureCameraController *con = [[SCCaptureCameraController alloc] init];
        con.scNaigationDelegate = self;
        con.iCardType = TIDXSZCARD;
        con.isDisPlayTxt = YES;
        [self.viewController presentViewController:con animated:YES completion:NULL];
    } else if (type == 2) { //驾驶证
        SCCaptureCameraController *con = [[SCCaptureCameraController alloc] init];
        con.scNaigationDelegate = self;
        con.iCardType = TIDJSZCARD;
        con.isDisPlayTxt = YES;
        [self.viewController presentViewController:con animated:YES completion:NULL];
    } else { //缺省身份证
        SCCaptureCameraController *con = [[SCCaptureCameraController alloc] init];
        con.scNaigationDelegate = self;
        con.iCardType = TIDCARD2;
        con.isDisPlayTxt = YES;
        con.ScanMode = TIDC_SCAN_MODE;
        [self.viewController presentViewController:con animated:YES completion:NULL];
    }
}

// 银行卡回调接口

- (void)sendBankCardInfo:(NSString *)bank_num BANK_NAME:(NSString *)bank_name BANK_ORGCODE:(NSString *)bank_orgcode BANK_CLASS:(NSString *)bank_class CARD_NAME:(NSString *)card_name
{
    NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"卡号  ： %@\n发卡行 ： %@\n机构代码： %@\n卡种  ： %@\n卡名  ： %@\n",bank_num,bank_name,bank_orgcode,bank_class,card_name]];
    NSLog(@"BANK INFO = %@\n",astring);
    [self.resultDict setObject:bank_num forKey:@"bank_num"];
    [self.resultDict setObject:bank_name forKey:@"bank_name"];
    [self.resultDict setObject:bank_orgcode forKey:@"bank_orgcode"];
    [self.resultDict setObject:bank_class forKey:@"bank_class"];
    [self.resultDict setObject:card_name forKey:@"card_name"];
    [self sendResponse:self.resultDict];
}
- (void)sendBankCardImage:(UIImage *)BankCardImage // 获取银行卡图片
{
    NSLog(@"name = %@",BankCardImage);
    NSString* filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"bankImage.jpeg"];
    [UIImageJPEGRepresentation(BankCardImage, 1.0) writeToFile:filePath atomically:YES];
    [self.resultDict setObject:filePath forKey:@"card_image"];
}

// end 银行卡回调接口

// 证件回调接口

// 获取证件识别的所有字段信息
- (void)sendAllValue:(NSString *)text
{
    NSLog(@"all  = %@",text);
}
// 获取身份证正面信息
- (void)sendIDCValue:(NSString *)name SEX:(NSString *)sex FOLK:(NSString *)folk BIRTHDAY:(NSString *)birthday ADDRESS:(NSString *) address NUM:(NSString *)num
{
    [self.resultDict setObject:name forKey:@"name"];
    [self.resultDict setObject:sex forKey:@"sex"];
    [self.resultDict setObject:folk forKey:@"folk"];
    [self.resultDict setObject:birthday forKey:@"birthday"];
    [self.resultDict setObject:address forKey:@"address"];
    [self.resultDict setObject:num forKey:@"num"];
}
// 获取身份证反面信息
- (void)sendIDCBackValue:(NSString *)issue PERIOD:(NSString *) period
{
    NSLog(@"idcback  = %@\n%@\n",issue,period);
    [self.resultDict setObject:issue forKey:@"issue"];
    [self.resultDict setObject:period forKey:@"period"];
    [self sendResponse: self.resultDict];
}
// 获取车牌号码
- (void)sendLPRValue:(NSString *)num
{
    NSLog(@"lpr  = %@",num);
    [self.resultDict setObject:num forKey:@"num"];
    [self sendResponse: self.resultDict];
}
// 获取行驶证识别信息
- (void)sendXSZValue:(NSString *)dp_plateno DP_TYPE:(NSString *)dp_type DP_OWNER:(NSString *)dp_owner DP_ADDRESS:(NSString *)dp_address DP_USECHARACTER:(NSString *) dp_usecharacter DP_MODEL:(NSString *)dp_model DP_VIN:(NSString *) dp_vin DP_ENGINENO:(NSString *)dp_engineno DP_REGISTER_DATE:(NSString *) dp_register_date DP_ISSUE_DATE:(NSString *)dp_issue_date
{
    [self.resultDict setObject:dp_plateno forKey:@"dp_plateno"];
    [self.resultDict setObject:dp_type forKey:@"dp_type"];
    [self.resultDict setObject:dp_owner forKey:@"dp_owner"];
    [self.resultDict setObject:dp_address forKey:@"dp_address"];
    [self.resultDict setObject:dp_usecharacter forKey:@"dp_usecharacter"];
    [self.resultDict setObject:dp_model forKey:@"dp_model"];
    [self.resultDict setObject:dp_vin forKey:@"dp_vin"];
    [self.resultDict setObject:dp_engineno forKey:@"dp_engineno"];
    [self.resultDict setObject:dp_register_date forKey:@"dp_register_date"];
    [self.resultDict setObject:dp_issue_date forKey:@"dp_issue_date"];
    [self sendResponse:self.resultDict];
    NSLog(@"xsz  = %@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n",dp_plateno,dp_type,dp_owner,dp_address,dp_usecharacter,dp_model,dp_vin,dp_engineno,dp_register_date,dp_issue_date);
}
// 获取驾驶证识别信息
- (void)sendJSZValue:(NSString *)dl_num DL_NAME:(NSString *)dl_name DL_SEX:(NSString *)dl_sex DL_COUNTRY:(NSString *) dl_country DL_ADDRESS:(NSString *)dl_address DL_BIRTHDAY:(NSString *)dl_birthday DL_ISSUE_DATE:(NSString *)dl_issue_date DL_CLASS:(NSString *)dl_class DL_VALIDFROM:(NSString *)dl_validfrom DL_VALIDFOR:(NSString *)dl_validfor
{
    [self.resultDict setObject:dl_num forKey:@"dl_num"];
    [self.resultDict setObject:dl_name forKey:@"dl_name"];
    [self.resultDict setObject:dl_sex forKey:@"dl_sex"];
    [self.resultDict setObject:dl_country forKey:@"dl_country"];
    [self.resultDict setObject:dl_address forKey:@"dl_address"];
    [self.resultDict setObject:dl_birthday forKey:@"dl_birthday"];
    [self.resultDict setObject:dl_issue_date forKey:@"dl_issue_date"];
    [self.resultDict setObject:dl_class forKey:@"dl_class"];
    [self.resultDict setObject:dl_validfrom forKey:@"dl_validfrom"];
    [self.resultDict setObject:dl_validfor forKey:@"dl_validfor"];
    [self sendResponse:self.resultDict];
    NSLog(@"all  = %@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n",dl_num,dl_name,dl_sex,dl_country,dl_address,dl_birthday,dl_issue_date,dl_class,dl_validfrom,dl_validfor);
}
//  获取火车票识别信息
- (void)sendTICValue:(NSString *)tic_start TIC_NUM:(NSString *)tic_num TIC_END:(NSString *)tic_end TIC_TIME:(NSString *)tic_time TIC_SEAT:(NSString *)tic_seat TIC_NAME:(NSString *) tic_name
{
    [self.resultDict setObject:tic_start forKey:@"tic_start"];
    [self.resultDict setObject:tic_num forKey:@"tic_num"];
    [self.resultDict setObject:tic_end forKey:@"tic_end"];
    [self.resultDict setObject:tic_time forKey:@"tic_time"];
    [self.resultDict setObject:tic_seat forKey:@"tic_seat"];
    [self.resultDict setObject:tic_name forKey:@"tic_name"];
    [self sendResponse:self.resultDict];
    NSLog(@"all  = %@\n%@\n%@\n%@\n%@\n%@\n",tic_start,tic_num,tic_end,tic_time,tic_seat,tic_name);
}
//获取身份证正面 人头像图片
- (void)sendCardFaceImage:(UIImage *)image
{
    if (image != NULL) {
        //保存图片
        NSString* filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"headImage.jpeg"];
        [UIImageJPEGRepresentation(image, 1.0) writeToFile:filePath atomically:YES];
        [self.resultDict setObject:filePath forKey:@"headImage"];
        [self sendResponse:self.resultDict];
    }
}
- (void)sendFieldImage:(TFIELDID) filed  image:(UIImage *)fieldimage
{
    
    NSLog(@"sendFieldImage = %d %@\n",filed,fieldimage);
}
//获取拍照的图片
- (void)sendTakeImage:(TCARD_TYPE) iCardType image:(UIImage *)cardImage
{
    NSLog(@"sendTakeImage = %d %@\n",iCardType,cardImage);
    NSString* filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"takeImage.jpeg"];
    [UIImageJPEGRepresentation(cardImage, 1.0) writeToFile:filePath atomically:YES];
    [self.resultDict setObject:filePath forKey:@"takeImage"];
}
- (void)sendIDCARDBackAction
{
    CDVPluginResult* result = [CDVPluginResult
                               resultWithStatus: CDVCommandStatus_ERROR
                               messageAsString: @"no shot,only return clicked"
                               ];
    
    [self.commandDelegate sendPluginResult:result callbackId:self.callback];
}
// end 证件回调接口

- (void)sendResponse:(NSMutableDictionary*)resultDict
{

    CDVPluginResult* result = [CDVPluginResult
                               resultWithStatus: CDVCommandStatus_OK
                               messageAsDictionary:resultDict
                               ];
    
    [[self commandDelegate] sendPluginResult:result callbackId: self.callback];
    self.resultDict = nil;
}


@end
